﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Portal.Nhibernate.Repository;

namespace Portal.Web.Controllers
{
    public class RecruitmentController : BaseFrontController
    {
        private readonly IRecruitmentRepository<IRecruitment> _recruitmentRepository;
        private readonly ICandidatesRepository<ICandidates> _candidatesRepository;

        public RecruitmentController(IRecruitmentRepository<IRecruitment> recruitmentRepository, ICandidatesRepository<ICandidates> candidatesRepository)
        {
            _recruitmentRepository = recruitmentRepository;
            _candidatesRepository = candidatesRepository;
        }

        public RecruitmentController()
        {
            _recruitmentRepository = new RecruitmentRepository();
            _candidatesRepository = new CandidatesRepository();
        }

        //
        // GET: /Recruitment/

        public ActionResult Index(string sort = "CreateTime", string order = "DESC", int page = 1, int rows = 10)
        {
            var recruitment = CreateFrontViewData<FronRecruitmentViewData>();
            recruitment.List = _recruitmentRepository.Get().Sorting(sort, order).ToPagedList(page, rows);
            return View(recruitment);
        }

        public ActionResult Details(string recruitmentId)
        {
            var recruitment = CreateFrontViewData<FronRecruitmentViewData>();
            recruitment.Model = _recruitmentRepository.Get().SingleOrDefault(o => o.Id == recruitmentId);
            if (recruitment.Model == null)
                return RedirectToAction("NotFount");
            return View(recruitment);
        }
        public ActionResult Candidates(string recruitmentId = "")
        {
            var recruitment = CreateFrontViewData<FronRecruitmentViewData>();
            recruitment.Model = _recruitmentRepository.Get().SingleOrDefault(o => o.Id == recruitmentId) ?? _recruitmentRepository.CreateEntity();
            return View(recruitment);
        }
        [HttpPost]
        public ActionResult Candidates(Candidates entity)
        {
            var model = _candidatesRepository.CreateEntity();
            model.Name = entity.Name;
            model.PostName = entity.PostName;
            model.Sex = entity.Sex;
            model.DateBirth = entity.DateBirth;
            model.MaritalStatus = entity.MaritalStatus;
            model.ContactPhone = entity.ContactPhone;
            model.ContactAddress = entity.ContactAddress;
            model.Email = entity.Email;
            model.Resume = entity.Resume;
            _candidatesRepository.Add(model);
            _candidatesRepository.Save();
            ViewData["Result"] = new AjaxResult { message = "应聘信息已提交，等待管理员审核！", state = true };
            return Candidates();
        }



        public ActionResult NotFount()
        {
            return View(CreateFrontViewData<FronRecruitmentViewData>());
        }
    }
}
